#!/usr/bin/bash
# Switches sww wallpaper
# Requires: coreutils, xrandr, hyprland

# Select and set image (hyprland)
cd "$HOME/Pictures"
imgpath=$(yad --width 1200 --height 800 --file --title='Choose wallpaper')
screensizey=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2 | head -1)
cursorposx=$(hyprctl cursorpos -j | gojq '.x')
cursorposy=$(hyprctl cursorpos -j | gojq '.y')
cursorposy_inverted=$(( screensizey - cursorposy ))

if [ "$imgpath" == '' ]; then
    echo 'Aborted'
    exit 0
fi

echo Sending "$imgpath" to swww. Cursor pos: ["$cursorposx, $cursorposy_inverted"] &
# Change swww wallpaper
swww img "$imgpath" --transition-fps 60 --transition-type random --transition-pos "$cursorposx, $cursorposy_inverted" &

# Generate colors for eww n stuff
ln -sf $imgpath $HOME/.config/eww/images/wallpaper
cd "$HOME/.config/eww" || exit
./scripts/launchapp '>wallcolor'
